﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Data.SqlClient;
using System.Threading;
using System.Diagnostics;
using System.IO;
using System.ComponentModel;
using System.Windows.Threading;

namespace SoundServant
{
    /// <summary>
    /// Interaction logic for LoadWindow.xaml
    /// </summary>
    public partial class SaveSkypeConfigurationWindow : SSWindow
    {
        Congregation congregation;
        SKYPE4COMLib.Skype skype;

        public SaveSkypeConfigurationWindow(Congregation _congregation, SKYPE4COMLib.Skype _skype)
        {
            
            skype = _skype;
            congregation = _congregation;

            this.ContentRendered += new EventHandler(SaveSkypeConfiguration_ContentRendered);
            InitializeComponent();
        }

        void SaveSkypeConfiguration_ContentRendered(object sender, EventArgs e)
        {
            MessageTextBlock.Text = "Shutting Skype Down";
           
            if (skype.Client.IsRunning)
            {
                skype.Client.Shutdown();
            }
            System.Windows.Forms.Application.DoEvents();
            int n = 0;

            while (((Process[])Process.GetProcessesByName("Skype.exe")).Length > 1)
            {
                MessageTextBlock.Text += ".";
                if (n >= 4 && n <= 6)
                {
                    MessageTextBlock.Text = "Killing Skype Process";
                    System.Windows.Forms.Application.DoEvents();
                    ((Process[])Process.GetProcessesByName("Skype.exe"))[0].Kill();
                }
                else if (n == 10)
                {
                    break;
                }
                Thread.Sleep(1000);
                n++;
            }

            if (!skype.Client.IsRunning)
            {
                MessageTextBlock.Text = "Copying Skype Configuration";
                System.Windows.Forms.Application.DoEvents();
                DirectoryInfo applicationData = new DirectoryInfo(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData));
                DirectoryInfo skypeApplicationData = new DirectoryInfo(applicationData.FullName + "\\Skype");
                if (skypeApplicationData.Exists)
                {
                    SS.CopyDirectory(skypeApplicationData, congregation.SkypeConfigDir);
                    MessageTextBlock.Text = "Configuration copied Successfully";
                }
                else
                {
                    MessageTextBlock.Text = "Skype Configuration Not Found";
                }
                System.Windows.Forms.Application.DoEvents();
                Thread.Sleep(500);
            }
            else
            {
                MessageTextBlock.Text = "Skype Process failed to shutdown, cannot copy Skype Configuration. Please consult your sound servant.";

                Logs.Information("Skype", "Skype Snapshot failed");
                Thread.Sleep(1000);
            }
            this.Closing -= Window_Closing;
            this.Close();
        }

        private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            e.Cancel = true;
        }

        public static void Execute(Congregation congregation, SKYPE4COMLib.Skype skype)
        {
            SaveSkypeConfigurationWindow saveSkypeConfigurationWindow = new SaveSkypeConfigurationWindow(congregation, skype);
            saveSkypeConfigurationWindow.ShowDialog();
        }

    }
}
